﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mesoBoard.Web.Core;
using mesoBoard.Data;

namespace mesoBoard.Web.Areas.Admin.Controllers
{
    public class SmiliesController : mesoBoardAdminController
    {
        [HttpGet]
        public ActionResult Smilies()
        {
            SetCrumb("Smilies");
            IEnumerable<Smiley> TheSmilies = Repositories.Smilies.GetSmilies();
            return View(TheSmilies);
        }

        [HttpPost]
        public ActionResult Smilies(Smiley TheSmiley)
        {
            SetCrumb("Smilies");

            if (ModelState.IsValid)
            {
                if (Repositories.Smilies.GetSmiley(TheSmiley.Code.ToLower()) != null)
                {
                    SetError("A smiley with this code already exists");
                }
                else
                {
                    Repositories.Smilies.CreateSmiley(TheSmiley);
                    SetSuccess("Smiley created");
                }
            }
            else
                SetError("Smiley Validation Error");
            IEnumerable<Smiley> TheSmilies = Repositories.Smilies.GetSmilies();
            return View(TheSmilies);
        }

        [HttpGet]
        public ActionResult EditSmiley(int SmileyID)
        {
            SetCrumb("Smilies");
            Smiley TheSmiley = Repositories.Smilies.GetSmiley(SmileyID);
            return View(TheSmiley);
        }

        public ActionResult DeleteSmiley(int SmileyID)
        {
            Repositories.Smilies.DeleteSmiley(SmileyID);
            SetSuccess("Smiley deleted");
            return RedirectToAction("Smilies");
        }

        [HttpPost]
        public ActionResult EditSmiley(Smiley TheSmiley, string Action)
        {
            if (ModelState.IsValid)
            {
                SetSuccess("Smiley saved");
                Repositories.Smilies.SaveSmiley(TheSmiley);
                return RedirectToAction("Smilies");
            }
            else
            {
                SetError("Smiley Validation Error");
                return View();
            }
        }
    }
}
